/**
*  Copyright 2009-2014 by WWW.KNET.CN  All rights reserved.
*/
package com.domain.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.domain.util.DateUtil;


/**
 * 附件上传
 * @author liujie
 * @date 2015年5月14日 上午10:16:33
 * 
 */
public class FileUploadController {
	@RequestMapping(value = "upload.shtml", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> uploadFile(MultipartFile file,
			HttpServletRequest request,String fileType) throws IOException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String prefixPath = request.getServletContext().getRealPath("/");
		String suffixPath = DateUtil.dateFormat(new Date(), "yyyyMMdd") + "/";
		String path = prefixPath + suffixPath;
		String newFileName = "";
		if (file.getSize() > 0) {
			File tempPath = new File(prefixPath + suffixPath);
			if (!tempPath.exists()) {
				tempPath.mkdirs();
			}

			newFileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
			path = path + newFileName;
			File updateFile = new File(path);
			file.transferTo(updateFile);
		}

		return resultMap;
	}
}
